package com.hbx.platform.org.service.impl;

import com.hbx.platform.base.service.impl.BaseServiceImpl;
import com.hbx.platform.org.domain.Department;
import com.hbx.platform.org.mapper.DepartmentMapper;
import com.hbx.platform.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author: hbx
 * @date: 2024/03/29
 **/
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void update(Department department) {
        department.setUpdateTime(new Date());
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void save(Department department) {
        department.setCreateTime(new Date()); //格式正确
        departmentMapper.save(department);//得到主键
        if(department.getParentId() == null){//顶级部门
            department.setPath("/"+department.getId());
        }else{//子部门
            Department queryById = queryById(department.getParentId());
            if(queryById.getPath() != null){ //path可能会为空
                department.setPath(queryById.getPath()+"/"+department.getId());
            }else{
                department.setPath("/"+department.getId());
            }
        }
        update(department);
    }


    @Override
    public List<Department> getTree() {
        return departmentMapper.getTree();
    }

    @Override
    public Department queryByName(String name) {
        return departmentMapper.queryByName(name);
    }
}
